/*
 * Copyright (C) Cátedra SAES-UMU 2011 <pedromateo@um.es>
 *
 * Android HCI Extractor is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Android HCI Extractor is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.mmi.android.instrumentation;

import java.util.LinkedList;

import org.mmi.android.instrumentation.utils.UiTraverser;
import org.mmi.facades.BaseMmiFacade;

import android.util.Log;
import android.view.View;

/**
 * The basis for instrumentation filters.
 * 
 * @author pedro mateo (pedromateo@um.es)
 *
 */
public abstract class BaseFilter 
implements InteractionListener {

	private static final String TAG = "BaseFilter";

	/**
	 * Constructor
	 * @param f facade to interact with the Multimodal Model Framework
	 */
	public BaseFilter(BaseMmiFacade f){
		//initialize variables
		mmim_ = f;
		subfilters_ = new LinkedList<BaseFilter>();
	}

	/**
	 * variables
	 */
	protected BaseMmiFacade mmim_;

	/**
	 * filter installing
	 */
	public abstract void install(View v);

	/**
	 * Installs the filter and subfilters in all widgets
	 * starting from top parent widget
	 * @param v
	 * @throws InstrumentationException 
	 */
	public void installOnTop(View v) throws InstrumentationException
	{
		if (v == null)
		{
			Log.d(TAG,"installOnTop :: view is null");
			return;
		}

		//look for the top view
		View topView = UiTraverser.getTopParent(v);
		if (topView == null)
			throw new InstrumentationException("BaseFilter::installOnTop :: topView is null");


		//install on the top view
		installOnView(topView);

		Log.d(TAG,"installOnTop :: Done");
	}

	/**
	 * Installs the filter and subfilters in all widgets
	 * starting from given widget
	 * @param v
	 */
	public void installOnView(View v)
	{
		if (v == null)
		{
			Log.d(TAG,"installOnView :: view is null");
			return;
		}
		//Log.d(TAG,"installOnView :: " + v.toString());

		UiTraverser uit = new UiTraverser(v);
		//Log.d(TAG,"installOnView");
		View aux = null;
		while (uit.hasNext())
		{
			aux = uit.next();
			assert(aux != null);

			///install process
			//install on me
			install(aux);
			//install on subfilters
			for(BaseFilter bf : subfilters_){
				bf.install(aux);
			}
		}
		Log.d(TAG,"installOnView :: Done");
	}


	///
	/// composite pattern
	///

	/**
	 * Set of sub-filters to implement a composite filter.
	 */
	protected LinkedList<BaseFilter> subfilters_;

	/**
	 * Adds a new filter to the sub-filters list.
	 * @param bf new filter to add
	 */
	public void addSubFilter(BaseFilter bf){
		subfilters_.addLast(bf);
	}

	/**
	 * Removes a filter to the sub-filters list (TODO)
	 * @param bf the filter to remove.
	 */
	public void removeSubFilter(BaseFilter bf){
		//TODO
	}
}
